package cn.lyjuan.payment.third.base.pay.chl.impl.ali;


import cn.lyjuan.payment.third.base.pay.chl.impl.AbstractPayChl;
import cn.lyjuan.payment.third.base.pay.chl.impl.ali.bean.*;
import cn.lyjuan.payment.third.base.pay.chl.impl.ali.enums.AliServiceEnum;
import cn.lyjuan.payment.third.base.pay.chl.impl.ali.util.*;
import cn.lyjuan.payment.third.base.pay.cst.PayConst;
import cn.lyjuan.payment.third.base.pay.enums.SimpleChlEnum;
import cn.lyjuan.payment.third.base.pay.enums.SimpleStatusEnum;
import cn.lyjuan.payment.third.base.pay.vo.req.NotifyReq;
import cn.lyjuan.payment.third.base.pay.vo.req.PayReq;
import cn.lyjuan.payment.third.base.pay.vo.req.QueryReq;
import cn.lyjuan.payment.third.base.pay.vo.req.TransferReq;
import cn.lyjuan.payment.third.base.pay.vo.res.*;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 支付宝支付渠道
 */
@Slf4j
@Component
public class AlipayChlImpl extends AbstractPayChl<AliChlCnf, AliPayReq, AliNotifyReq, QueryReq.IQueryReq, AlipayTransferReq, AliPayMapRes> {

    @Autowired
    private PayConst payConst;

    @Override
    public SimpleChlEnum chl() {
        return SimpleChlEnum.ALI;
    }

    @Override
    public BaseRes checkPayParams(AliPayReq params) {
        return AlipaySubmit.checkPayReq(params);
    }

    @Override
    public BaseRes checkTransferParams(AlipayTransferReq params) {
        return AlipaySubmit.checkTransferReq(params);
    }

    @Override
    public QueryReq.IQueryReq parseQueryParams(Map<String, String> params) {
        // 不需要
        return null;
    }

    @Override
    public AliNotifyReq parseNotifyParams(HttpServletRequest req) {
        return AlipayNotify.parseNotifyParams(req);
    }


    @Override
    public PayRes<AliPayMapRes> order(PayReq<AliPayReq> req, AliChlCnf cnf) {
        AliPayMapRes res = null;
        // make web_qrcode pay form str
        if (req.getParams().getService().equals(AliServiceEnum.WEB_QRCODE)) {
            res = AlipaySubmit.getWebQrCodeForm(req, cnf, payConst.isDev());
        }
        // 创建支付宝WAP支付交易信息
        if (req.getParams().getService().name().equals("WAP")) {
            res = AliWapPaySubmit.createTradeExec(req, cnf, payConst.isPayDebug());
        }
        // 创建支付宝APP支付交易信息
        if (req.getParams().getService().name().equals("APP")) {
            res = AlipaySubmit.createTradeExec(req, cnf, payConst.isPayDebug());
        }

        return new PayRes(req.getLocalNo(), res);
    }

    @Override
    public TransferRes<AliPayMapRes> transfer(TransferReq<AlipayTransferReq> req, AliChlCnf cnf) {
        AliPayMapRes res = AlipaySubmit.transferExec(req, cnf, payConst.isTransferDebug());
        if (res.getSucc()) {
            return new TransferRes<>(req.getLocalNo(), res);
        }
        return new TransferRes<>(false, res.getOrderStr());
    }

    @Override
    public NotifyRes notifyStatus(NotifyReq<AliNotifyReq> req, AliChlCnf cnf) {
        // 签名验证
        boolean signMatch = AlipayNotify.veryfyNotifySign(req.getParams(), cnf);

        if (!signMatch)// 签名不匹配
        {
            log.warn("Ali notify sign mismatch: localNo-{}", req.getLocalNo());
            return new NotifyRes(SimpleStatusEnum.UNKNOWN, AlipayNotify.notifyRes(false));
        }

        // 交易状态
        SimpleStatusEnum status = AlipayNotify.parseNotifyPayStatus(req.getParams());
        // 状态描述
        String chlStatus = AlipayNotify.notifyPayStatus(req.getParams());
        // 支付平台流水号
        String tranNo = AlipayNotify.parseTranNoFromNotify(req.getParams());

        return new NotifyRes(status, chlStatus, tranNo, AlipayNotify.notifyRes(true));
    }

    @Override
    public RefundRes orderRefund(String selfRefundNo, String localNo, Integer totalAmt, Integer amt, AliChlCnf cnf) {
        AlipayTradeRefundResponse response = AlipayRefund.createTradeExec(selfRefundNo, localNo, amt, cnf, false);
        if (response.getCode().equals("10000")) {
            return new RefundRes(SimpleStatusEnum.SUCC, "SUCC", response.getTradeNo());
        }
        return new RefundRes(SimpleStatusEnum.FAIL, response.getMsg() + ":" + response.getSubMsg());
        //return null;
    }

    @Override
    public QueryRes queryStatus(QueryReq req, AliChlCnf cnf) {
        // todo 实现支付宝查询
        AlipayTradeQueryResponse res = AlipayQuery.query(req.getLocalNo(), cnf, payConst.isPayDebug());

        SimpleStatusEnum status = AlipayQuery.parseStatus(res);

        String msg = AlipayQuery.parseMsg(res);

        return new QueryRes(status, msg, res.getTradeNo(), null);
//        return null;
    }

    public PayConst getPayConst() {
        return payConst;
    }

    public void setPayConst(PayConst payConst) {
        this.payConst = payConst;
    }
}
